import random
from flask import Blueprint, request
from conn_mysql import ConnectMysql


app_phone_notify = Blueprint('app_phone_notify', 'api')

def send_sms(phone):#发送短信

    sms_code = random.randint(100000,999999)

    with ConnectMysql() as c:

        sql = "INSERT INTO sms(phone, verify_code) VALUES (%s, %s)"

        c.insert_info(sql, (phone, sms_code))

    print(f'给电话：{phone},已经发送短信，短信验证码为{sms_code}')



@app_phone_notify.route('/app_phone_notify', methods=['POST'])
def phone_notify():
    phone = request.form.get('phone')
    send_sms(phone)

    return {
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": "短信已经发送到你的手机"
    }
